﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Topshelf;

namespace WindowsFormsTest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            try
            {
                //http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-configuration
                // 配置和运行宿主服务
                HostFactory.Run(x =>                                 //1
                {
                    x.Service<Runtest>(s =>                        //2
                    {
                        // 指定服务类型。这里设置为 Service
                        s.ConstructUsing(name => new Runtest());     //3
                        // 当服务启动后执行什么
                        s.WhenStarted(tc => tc.runcj());              //4
                        // 当服务停止后执行什么
                        //s.WhenStopped(tc => tc.Stop());               //5    
                    });
                    x.StartAutomatically();
                    // 服务用本地系统账号来运行
                    x.RunAsLocalSystem();                            //6
                    x.EnableShutdown();
                    // 服务描述信息
                    x.SetDescription("定时服务");        //7
                    // 服务显示名称
                    x.SetDisplayName("aaa");                       //8
                    // 服务名称
                    x.SetServiceName("aaa");                       //9 
                    x.EnableServiceRecovery(y => {
                        y.RestartService(0);
                        y.RestartService(0);
                        y.RestartService(1);
                        y.OnCrashOnly();
                        y.RunProgram(1, "ping baidu.com -t");
                        y.SetResetPeriod(1); //失败次数1天重新计数
                    });
                });
            }
            catch (Exception ex)
            {  
                Console.WriteLine(ex);
            }
        }
    }
}
